<?php //pack11class

class LeftMenuAll extends Module {

    private $current;
    private $sqlQuery;

    private $labelPlaced=false;
    private $group=array();

	public function setup() {
      $this->setTemplate("LeftMenuTemplate");
      $this->traceEnabled=true;
      $label=new LeftMenuLabel;
      $label->setBody(LabelManager::getLabel("all_objects_label"));
      $this->addChild($label);

        $objects=MySQLGet::process("Select * from ".MySQL::TABLE_OBJECTS." where `active`='1';");
        foreach($objects as $row) {
            foreach(PropertiesManager::getGroup(2) as $property) {
                if(!is_null($row[$property->field])) {
                    if(array_key_exists($property->system.$row[$property->field], $this->group)) {
                        $this->group[$property->system.$row[$property->field]]["count"]++;
                    } else {
                        $this->group[$property->system.$row[$property->field]]=array("value"=>$row[$property->field],"count"=>1,"property"=>$property);
                    }
                    break;
                 }
            }
        }

        foreach($this->group as $row) {
               //$value_sql=MySQLGet::process("Select ".LanguagesManager::sql()." from ".MySQL::TABLE_VALUES." where `id`='{$row["value"]}' limit 1;");
               $value=ValuesManager::getValueByID($row["value"]);
               $property=clone $row["property"];
               $count=$row["count"];
               $item=new LeftMenuItem;

                $property->value=$row["value"];
                $queryObject=new GetQueryObject;
                $queryObject->setValue($property);

                 $getquery=GetQuery::generate($queryObject);
                $item->link="?state=".$getquery;
                $item->setBody($value);
                $item->count=$count;

                $this->addChild($item);

        }
      

      //$this->draw(PropertiesManager::getProperty("country"));

    }

    private function draw(Property $property) {

        if($property->isValue) return;

        $table=MySQL::TABLE_OBJECTS;
        $join=MySQL::TABLE_VALUES;
        $lang=LanguagesManager::sql();

        $_query=$this->generateSQLQuery($property);
        //trace($_query);

        //Select `b`.`total`, count(`a`.`prop5`) as `count`, `a`.`prop5` as `valueid`, `c`.`lang0` as `value`  from `d12_objects` a left join `d12_values` c on `c`.`id`=`a`.`prop5` left join (Select `id`, count(`id`) as `total` from `d12_objects`) b on `b`.`total`=`b`.`total` where `a`.`prop5` is not null group by `value`

        while(!MySQLGet::process("Select count({$table}.`{$property->field}`) as `count`, {$table}.`{$property->field}` as `valueid`, {$join}.{$lang}  from {$table} left join {$join} on ({$join}.`id`={$table}.`{$property->field}`) {$_query} group by `value`;")) {
            //trace(MySQLGet::getQuery()." ".((MySQLGet::$result==false)? "false":MySQLGet::$result));
            $property=PropertiesManager::getChild($property);
            if(!$property) {
                trace("next child not found");
                return;
            }
            $_query=$this->generateSQLQuery($property);
        }
            //trace(MySQLGet::getQuery());

        $result=MySQLGet::$result;
        foreach($result as $row) {
            $item= new LeftMenuItem;
            //trace("ValueID: ".(is_null($row["valueid"])? "null":$row["valueid"]));
            if(is_null($row["valueid"])) {
                trace("!!!NULL!!!");
                array_push($this->sqlQuery,"`{$property->field}` is null");
                $this->draw(PropertiesManager::getChild($property));
            } else {
                $_property=clone $property;
                $_property->value=$row["valueid"];
                $queryObject=new GetQueryObject;
                $queryObject->setValue($_property);

                 $getquery=GetQuery::generate($queryObject);
                $item->link="?state=".$getquery;
                GetQuery::translate($getquery);
                $item->setBody($row["value"]);
                $item->count=$row["count"];

                $this->addChild($item);
            }
        }
    }

    private function generateSQLQuery(Property $property) {
        $table=MySQL::TABLE_OBJECTS;
        return (count($this->sqlQuery)>0)? "where ".implode(" AND ",$this->sqlQuery)." AND `active`='1'":"where {$table}.`{$property->field}` AND `active`='1'";
    }

}
?>
